<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

if(!isset($_SESSION['install']['database'])) redirect('?step=database');
if(!isset($_SESSION['install']['paths'])) redirect('?step=paths');

if(count($_POST)>0){
    $error = false;

    $structure = post('structure');
    $data = post('data');
    $email = post('email');
    $username = post('username');
    $password = post('password');
    $password2 = post('password2');
    $cryptSalt = str_replace("'", "\'", post('cryptSalt'));
    $sitename = post('sitename');

    if(strlen($cryptSalt)<8){
        $error = true;
        $errors[] = 'Crypt salt must be 8 characters or more';
    }
    if(strlen($password)<6){
        $error = true;
        $errors[] = 'Password must be 6 characters or more';
    }
    if($password != $password2){
        $error = true;
        $errors[] = 'Password confirmation does not match first password';
    }

    if(!$error){
        $_SESSION['install']['settings'] = array(
            'structure' => $structure,
            'data' => $data,
            'username' => $username,
            'email' => $email,
            'password' => $password,
            'cryptSalt' => $cryptSalt,
            'sitename' => $sitename
        );

        redirect('?step=review');
    }

}

// get our default values
$dirname = str_replace(DIRECTORY_SEPARATOR, '/', dirname(__FILE__));
$dirname = explode('/', $dirname);
array_pop($dirname);
$baseDir = implode('/', $dirname);

$publicDir = str_replace(DIRECTORY_SEPARATOR, '/', rtrim(dirname(JFX_EXEC_FILE), DIRECTORY_SEPARATOR));

$requestUri = str_replace('index.php', '', $_SERVER['REQUEST_URI']);
$requestUri = str_replace('//', '/', $requestUri);
$requestUri = explode('?', $requestUri);
$requestUri = $requestUri[0];
$pathPrefix = rtrim($requestUri, '/').'/';
$baseUrl = rtrim('http://'.$_SERVER['SERVER_NAME'].'/'.$request_uri, '/');

$adminUrl = $baseUrl.'/admin';

$chars = array(
    2, 3, 4, 6, 7, 8, 9, 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h',
    'H', 'i', 'j', 'J', 'k', 'K', 'L', 'm', 'M', 'n', 'N', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S',
't', 'T', 'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z'
);
$cryptSalt = '';
while(strlen($cryptSalt)<10){
    $cryptSalt .= $chars[array_rand($chars)+rand(0, 9)];
}
$cryptSalt = trim($cryptSalt);
?>
<form method="post">
    <div class="form-field">
        <label>Site Name</label>
        <input type="text" name="sitename" class="wide" value="<?php echo post('sitename', 'My JFX Website') ?>" />
    </div>
    <div class="form-field">
        <label>Crypt Salt</label>
        <input type="text" name="cryptSalt" class="wide" value="<?php echo post('cryptSalt', $cryptSalt) ?>" />
    </div>
    <div class="form-field">
        <label>Administrator Email</label>
        <input type="text" name="email" class="wide" value="<?php echo post('email', 'admin@'.$_SERVER['SERVER_NAME']) ?>" />
    </div>
    <div class="form-field">
        <label>Administrator Username</label>
        <input type="text" name="username" value="<?php echo post('username', 'admin') ?>" />
    </div>
    <div class="form-field">
        <label>Administrator Password</label>
        <input type="password" name="password" value="<?php echo post('password', '') ?>" />
    </div>
    <div class="form-field">
        <label>Confirm Password</label>
        <input type="password" name="password2" value="<?php echo post('password2', '') ?>" />
    </div>
    <div class="form-field">
        <input type="checkbox" id="structure" name="structure" value="1" <?php echo showChecked('structure', '1', true) ?> />
        <label class="checkbox" for="structure">Install Database Structure (Deletes existing tables)</label>

    </div>
    <div class="form-field">
        <input type="checkbox" id="data" name="data" value="1" <?php echo showChecked('data', '1', true) ?> />
        <label class="checkbox" for="structure">Install Sample Data (Deletes existing data)</label>
    </div>
    <div class="form-field">
        <label>&nbsp;</label>
        <input type="submit" name="submit" value="Proceed" />
    </div>

</form>


